
global data_folder_final "W:\intimate\data"
global log_folder "W:\intimate\dofiles\logs"
global result_folder "W:\intimate\results_revision"

use "W:\intimate\data\allmen_data_clean_withDV", replace
forvalues i = 1/5 {
	ren ptoim1F`i' man_ptoim1F`i'
}
ren ptoim10 man_ptoim10
ren ptoim1B1 man_ptoim1B1
keep shnro year_start_cohab man_*
ren shnro sphnro0
duplicates drop
tempfile man_emp
save `man_emp'

use "W:\intimate\data\match_victim_data_clean_new", clear
merge m:1 sphnro0 year_start_cohab using `man_emp'
keep if _merge ==3
drop _merge 

// Analysis 
g av_prior_inc = (tyotuB5 + tyotuB4 +  tyotuB3 + tyotuB2 + tyotuB1)/5
g nchild_pre = nchildB1
g nchild_early = nchildF2
g emp_0 = ptoim10
g nochild_0=(nchild0==0)

g perp_emp_00 = (spouse_pre_ptoim1 == 0 & man_ptoim10 == 0)
g perp_emp_01 = (spouse_pre_ptoim1 == 0 & man_ptoim10 == 1)
g perp_emp_10 = (spouse_pre_ptoim1 == 1 & man_ptoim10 == 0)
g perp_emp_11 = (spouse_pre_ptoim1 == 1 & man_ptoim10 == 1)

g perp_emp_status = .
replace perp_emp_status = 1 if (spouse_pre_ptoim1 == 0 & man_ptoim10 == 0)
replace perp_emp_status = 2 if (spouse_pre_ptoim1 == 0 & man_ptoim10 == 1)
replace perp_emp_status = 3 if (spouse_pre_ptoim1 == 1 & man_ptoim10 == 0)
replace perp_emp_status = 4 if (spouse_pre_ptoim1 == 1 & man_ptoim10 == 1)


local varlist sphnro ptoim1 saiprva tyotu nchild info_missing same_spouse
g same_spouse0 = 1
forvalues i = 1/5 {
	g same_spouseB`i' = (sphnro0 == sphnroB`i')
}
forvalues i = 1/5 {
	g same_spouseF`i' = (sphnro0 == sphnroF`i')
}

forvalues i = 1/5 {
    local j = 6 -`i'
    foreach thing in `varlist' {
		ren `thing'B`i' `thing'`j'
	}
}

foreach thing in `varlist' {
    ren `thing'0 `thing'6
}

forvalues i = 1/5 {
    local j = 6 +`i'
    foreach thing in `varlist' {
		ren `thing'F`i' `thing'`j'
	}
}

g group_id = _n



reshape long `varlist', i(group_id) j(time)

*Define dummies used in the event studies
drop year year_event
g time_ = time-6
egen time_year_cohab = group(time_ year_start_cohab)
g year = year_start_cohab + (time - 6)
gen treat= dv_couple==1

*drop baseid

*Time displacement dummies
g dpl_5=time_==-5 & treat==1
g dpl_4=time_==-4 & treat==1
g dpl_3=time_==-3 & treat==1
g dpl_2=time_==-2 & treat==1
g dpl_1=time_==-1 & treat==1
g dpl_0=time_==0 & treat==1
gen dpl1=time_==1 & treat==1
gen dpl2=time_==2 & treat==1
gen dpl3=time_==3 & treat==1
gen dpl4=time_==4 & treat==1
gen dpl5=time_==5 & treat==1

gen treatPost= treat==1 & time_>=0
egen full_fe = group(match_id time)
global dummies =  "dpl_5 dpl_4 dpl_3 dpl_2 dpl_0 dpl1 dpl2 dpl3 dpl4 dpl5"
global fe = "full_fe year_start_cohab" 
global cluster = "match_id"

g inc_diff = (tyotu - av_prior_inc)/av_prior_inc
replace inc_diff = 5 if inc_diff >5 & missing(inc_diff)==0

lab var treatPost "Abusive x Cohabitation"

reghdfe ptoim1  treatPost treat,  absorb($fe)  cluster($cluster)
est sto main_effect
reghdfe ptoim1  treatPost treat if man_ptoim1B1==1,  absorb($fe)  cluster($cluster)
est sto perp_1
reghdfe ptoim1  treatPost treat if man_ptoim1B1==0,  absorb($fe)  cluster($cluster)
est sto perp_0
reghdfe ptoim1  treatPost treat if man_ptoim1B1==1 &  man_ptoim10==1,  absorb($fe)  cluster($cluster)
est sto perp_11
reghdfe ptoim1  treatPost treat if man_ptoim1B1==1 &  man_ptoim10==0,  absorb($fe)  cluster($cluster)
est sto perp_10
reghdfe ptoim1  treatPost treat if man_ptoim1B1==0 &  man_ptoim10==1,  absorb($fe)  cluster($cluster)
est sto perp_01
reghdfe ptoim1  treatPost treat if man_ptoim1B1==0 &  man_ptoim10==0,  absorb($fe)  cluster($cluster)
est sto perp_00

coefplot (perp_11, label("Spouse Employed at -1") bcolor(red)) (perp_01,  label("Spouse Non-employed at -1") bcolor(blue)), recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1_1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) title("Spouse Employed at 0") ///
			yscale(r(-.14 0)) ylabel(-0.14(0.02)0) yline(0) ytitle("Employment")

coefplot (perp_10, label("Spouse Employed at -1") bcolor(red)) (perp_00,  label("Spouse Non-employed at -1") bcolor(blue)), recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1_0, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) title("Spouse Non-employed at 0") ///
			yscale(r(-.14 0)) ylabel(-0.14(0.02)0) yline(0) ytitle("Employment")
			
grc1leg ptoim1_1 ptoim1_0, rows(1)  graphregion(color(white))
graph export "$result_folder\victim_match_het_maleshock0.pdf", replace

reghdfe ptoim1  treatPost treat if man_ptoim1B1==1 &  man_ptoim1F1==1,  absorb($fe)  cluster($cluster)
est sto perp_11
reghdfe ptoim1  treatPost treat if man_ptoim1B1==1 &  man_ptoim1F1==0,  absorb($fe)  cluster($cluster)
est sto perp_10
reghdfe ptoim1  treatPost treat if man_ptoim1B1==0 &  man_ptoim1F1==1,  absorb($fe)  cluster($cluster)
est sto perp_01
reghdfe ptoim1  treatPost treat if man_ptoim1B1==0 &  man_ptoim1F1==0,  absorb($fe)  cluster($cluster)
est sto perp_00

coefplot (perp_11, label("Spouse Employed at -1") bcolor(red)) (perp_01,  label("Spouse Non-employed at -1") bcolor(blue)), recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1_1, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) title("Spouse Employed at 1") ///
			yscale(r(-.14 0)) ylabel(-0.14(0.02)0) yline(0) ytitle("Employment")

coefplot (perp_10, label("Spouse Employed at -1") bcolor(red)) (perp_00,  label("Spouse Non-employed at -1") bcolor(blue)), recast(bar) citop ciopts(recast(rcap)) barwidth(0.2) ///
			keep(treatPost) name(ptoim1_0, replace) ///
			vertical legend(rows(1)) graphregion(color(white)) title("Spouse Non-employed at 1") ///
			yscale(r(-.14 0)) ylabel(-0.14(0.02)0) yline(0) ytitle("Employment")
			
grc1leg ptoim1_1 ptoim1_0, rows(1)  graphregion(color(white))
graph export "$result_folder\victim_match_het_maleshock1.pdf", replace

